package ys.mihoyo.ymcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import ys.mihoyo.ymcc.domain.CourseType;
import ys.mihoyo.ymcc.mapper.CourseTypeMapper;
import ys.mihoyo.ymcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author mihoyo
 * @since 2022-12-06
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Resource
    private RedisTemplate<String, List<CourseType>> redisTemplate;
    //课程类别树在redis中的key
    private static final String KEY_TREE = "courseType:";

    @Override
    @Cacheable(cacheNames = KEY_TREE,key = "'ALL'")
    public List<CourseType> treeData() {
        //在Redis中查询是否有无限极树
       /* if (redisTemplate.opsForValue().get(KEY_TREE)!=null){
            return redisTemplate.opsForValue().get(KEY_TREE);
        }*/
        //获取数据
        List<CourseType> courseTypes = selectList(null);
        //courseTypes.forEach(System.out::println);
        /*//创建一个map类储存元素
        HashMap<Long, CourseType> map = new HashMap<>();
        //循环添加数据
        for (CourseType courseType : courseTypes) {
            map.put(courseType.getId(),courseType);
        }*/
        Map<Long,CourseType> map = courseTypes.stream().collect(Collectors.toMap(CourseType::getId,courseType -> courseType ));
        //创建集合准备拼接
        List<CourseType> treeData = new ArrayList<>();
        for (CourseType courseType : courseTypes) {
            //没有子部门就是顶级父部门
            if (courseType.getPid()==0){
                treeData.add(courseType);
            }else {
                //其余的就是有父级部门的，查出他们的父亲
                Long cid = courseType.getPid();
                //通过id在map中找到父级部门
                CourseType fatherType = map.get(cid);
                //非空判断
                if (fatherType != null){
                    //封装到父部门内
                    fatherType.getChildren().add(courseType);
                }
            }
        }
        //treeData.forEach(System.out::println);
        redisTemplate.opsForValue().set(KEY_TREE,treeData,3, TimeUnit.DAYS);
        return treeData;

    }

    @Override
    @Cacheable(cacheNames = KEY_TREE,key = "'ALL'")
    public boolean insert(CourseType entity) {
        boolean insert = super.insert(entity);
       /* //移除Redis
        if (insert){
            redisTemplate.delete(KEY_TREE);
        }*/
        return insert;
    }

    @Override
    @Cacheable(cacheNames = KEY_TREE,key = "'ALL'")
    public boolean updateById(CourseType entity) {
        boolean b = super.updateById(entity);
        /*if (b){
            //移除Redis
            redisTemplate.delete(KEY_TREE);
        }*/
        return  b;
    }

    @Override
    @Cacheable(cacheNames = KEY_TREE,key = "'ALL'")
    public boolean deleteById(Serializable id) {
        boolean b = super.deleteById(id);
       /* if (b){
            //移除Redis
            redisTemplate.delete(KEY_TREE);
        }*/
        return b;
    }
}
